package com.nativetalk.controller.app.three.index;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.dict.TsNtDictIndex;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.three.index.ThreeIndexService;
import com.nativetalk.service.three.search.SearchService;
import com.nativetalk.service.three.teacher.search.ThreeTeacherSearchService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeIndexController {
    private static Logger logger = Logger.getLogger(ThreeIndexController.class);
    @Resource
    private ThreeIndexService threeIndexService;
    @Resource
    private ThreeTeacherSearchService threeTeacherSearchService;
    @Resource
    private SearchService searchService;
    /**
     * @Purpose  首页(banner列表,直播预告列表,推荐老师列表,老师列表)
     * @version  3.2
     * @time     2015-08-23
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/8/index", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo index( @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            retInfo = threeIndexService.index(tdNtMember);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @Purpose  查找老师列表(obj0全部1中教2外教)
     * @version  3.2
     * @time     2015-08-23
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/8/index/teacher/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teacherList(@RequestBody Page page, @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            page.setPageSize(9);
            retInfo = threeIndexService.teacherList(page,tdNtMember);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @Purpose  查找详情
     * @version  3.2
     * @time     2015-08-23
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/8/teacher/detail", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo detail(@RequestBody Map<String, Integer> map,
                          @RequestHeader(value = "token", required = false) String token,
                          @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            TdNtMember tdNtMember = null;

            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isNotBlank(token)) {
                Object o = MemcachedUtils.get(token);
                if (o != null && TdNtMember.class.isInstance(o)) {
                    tdNtMember = (TdNtMember) o;
                }
            }
            retInfo = threeTeacherSearchService.findTeacherDetail(map.get("tea_id"),tdNtMember,version);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @Purpose  查找前十条搜索记录
     * @version  3.2
     * @time     2015-08-24
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/8/search/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo list(@RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            retInfo = searchService.findSearchByMembId(tdNtMember);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @Purpose  获取所有学校信息
     * @version  3.2
     * @time     2015-08-24
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/8/university/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo university(@RequestHeader(value = "token", required = false) String token) {
        RetInfo retInfo = new RetInfo();
        Map<String , Object> map = new HashMap<>();
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            List<TsNtDictIndex> university = (List<TsNtDictIndex>) MemcachedUtils.get(MemcachedKey.tsNtDictIndices);
            map.put("university" , university);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取大学列表数据成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @Purpose  搜索老师
     * @version  3.2
     * @time     2015-08-24
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/8/teacher/search/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo search(@RequestBody Page page,@RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            page.setPageSize(9);
            retInfo = threeIndexService.searchTeacherList(page,tdNtMember);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @Purpose  查找详情(直播用)
     * @version  3.2
     * @time     2015-08-23
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/8/token/live/teacher/detail", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo livedetail(@RequestBody Map<String, Integer> map,
                          @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            retInfo = threeTeacherSearchService.findTeacher(map.get("tea_id"),tdNtMember);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}


